//
//  TJCreateEmployeeController.m
//  BaoDian
//
//  Created by mac on 2024/7/27.
//

#import "TJCreateStaffController.h"
#import "TJCreateEmployeeInputView.h"
#import "TJButton.h"
#import "QMUITextField.h"


@interface TJCreateStaffController ()<QMUITextFieldDelegate>

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, weak) TJButton *confirmButton;
@property (nonatomic, weak) TJCreateEmployeeInputView *nameInputView;
@property (nonatomic, weak) TJCreateEmployeeInputView *accountInputView;
@property (nonatomic, weak) TJCreateEmployeeInputView *passwordInputView;

@end

@implementation TJCreateStaffController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds].addTo(self.view);
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [maskView addTarget:self action:@selector(didClickMaskButton) forControlEvents:UIControlEventTouchUpInside];
    _maskView = maskView;
    
    UIView *contentView = [UIView addToView:maskView];
    contentView.backgroundColor = UIColorMake(0x1E1F23);
    _contentView = contentView;
    
    UIView *titleView = [UIView addToView:contentView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.right.offset(0);
        make.height.offset(70);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:titleView font:UIFontMediumMake(16) color:UIColor.whiteColor title:@"创建" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    TJCreateEmployeeInputView *nameInputView = [TJCreateEmployeeInputView addToView:contentView];
    nameInputView.addCornerRadius(8);
    nameInputView.titleLabel.text = @"*员工名称";
    nameInputView.textField.placeholder = @"请输入您员工的名称";
    nameInputView.textField.delegate = self;
    nameInputView.titleLabel.attributedText = [nameInputView.titleLabel.text attributedStringWithRangeString:@"*" rangeColor:UIColorMake(0xEF382D) color:UIColor.whiteColor];
    nameInputView.backgroundColor = UIColorMake(0x2A2B31);
    [nameInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.height.offset(50);
        make.top.equalTo(titleView.mas_bottom).offset(8);
    }];
    
    TJCreateEmployeeInputView *accountInputView = [TJCreateEmployeeInputView addToView:contentView];
    accountInputView.addCornerRadius(8);
    accountInputView.titleLabel.text = @"*登录账号";
    accountInputView.textField.placeholder = @"请输入您需要创建登录账号";
    accountInputView.textField.delegate = self;
    accountInputView.textField.keyboardType = UIKeyboardTypeNumberPad;
    accountInputView.titleLabel.attributedText = [accountInputView.titleLabel.text attributedStringWithRangeString:@"*" rangeColor:UIColorMake(0xEF382D) color:UIColor.whiteColor];
    accountInputView.backgroundColor = UIColorMake(0x2A2B31);
    [accountInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(nameInputView);
        make.top.equalTo(nameInputView.mas_bottom).offset(12);
    }];
    
    TJCreateEmployeeInputView *passwordInputView = [TJCreateEmployeeInputView addToView:contentView];
    passwordInputView.addCornerRadius(8);
    passwordInputView.titleLabel.text = @"*登录密码";
    passwordInputView.textField.placeholder = @"请输入您需要创建登录密码";
    passwordInputView.textField.delegate = self;
    passwordInputView.textField.secureTextEntry = YES;
    passwordInputView.titleLabel.attributedText = [passwordInputView.titleLabel.text attributedStringWithRangeString:@"*" rangeColor:UIColorMake(0xEF382D) color:UIColor.whiteColor];
    passwordInputView.backgroundColor = UIColorMake(0x2A2B31);
    [passwordInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(nameInputView);
        make.top.equalTo(accountInputView.mas_bottom).offset(12);
    }];
 
    TJButton *cancelButton = [TJButton buttonWithSuperView:contentView font:UIFontMake(16) color:UIColor.whiteColor title:@"取消"];
    [cancelButton addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.inset(TabbarSafeBottomMargin);
        make.height.offset(54);
        make.left.right.inset(0);
    }];
    
    UIView *marginView = [UIView addToView:contentView];
    marginView.backgroundColor = UIColorMake(0x2A2B31);
    [marginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.height.offset(8);
        make.bottom.equalTo(cancelButton.mas_top);
    }];
    
    TJButton *confirmButton = [TJButton buttonWithSuperView:contentView font:UIFontMake(16) color:UIColorMake(0x2978F0) title:@"确认"];
    confirmButton.addCornerRadius(12);
//    confirmButton.enabled = NO;
    [confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(cancelButton);
        make.bottom.equalTo(marginView.mas_top);
    }];
    
    UIView *lineView = [UIView addToView:contentView];
    lineView.backgroundColor = UIColorMake(0x2A2B31);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.height.offset(1);
        make.bottom.equalTo(confirmButton.mas_top);
    }];
    
    CGFloat contentViewH = TabbarSafeBottomMargin + 54 * 2 + 8 + 289;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, contentViewH);
    [contentView addRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:20];
    
    _accountInputView = accountInputView;
    _nameInputView = nameInputView;
    _passwordInputView = passwordInputView;
}

- (void)textFieldDidChange:(UITextField *)textField {
    _confirmButton.enabled = !(isEmptyString(self.nameInputView.textField.text) || isEmptyString(self.accountInputView.textField.text) || isEmptyString(self.passwordInputView.textField.text));
}
 
- (void)didClickConfirmButton {
    [TJHUD showLoadingInView:self.view];
    [TJMinePresent createStaffWithGroupId:self.groupId account:self.accountInputView.textField.text password:self.passwordInputView.textField.text realName:self.nameInputView.textField.text complete:^(NSError * _Nullable error) {
        if (error) {
            return;
        }
        
        [self dismissComplete:^{
            if (self.complete) {
                self.complete();
            }
        }];
    }];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didClickMaskButton {
    [self.view endEditing:YES];
}
 
- (void)didClickDismissButton {
    [self dismissComplete:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
        self.maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
    }];
    
}

- (void)dismissComplete:(void (^)(void))complete {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) {
                complete();
            }
        }];
    }];
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationCustom;
}


@end

